const { Sequelize } = require('sequelize');
require('dotenv').config();
const path = require('path');

// 创建 Sequelize 实例 - 使用SQLite
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: path.join(__dirname, '../database.sqlite'),
  logging: process.env.NODE_ENV === 'development' ? console.log : false,
  define: {
    timestamps: true,
    underscored: false,
    freezeTableName: true
  }
});

// 测试数据库连接
async function testConnection() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接测试成功 (SQLite)');
    return true;
  } catch (error) {
    console.error('数据库连接失败:', error);
    return false;
  }
}

module.exports = {
  sequelize,
  testConnection
}; 